const App = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        isLogin: false,
        userInfo: {}, // 用户信息
        orderCount: {}, // 订单数量
        menu: {
            myOrder: {
                name: '我的订单',
                url: 'pages/order/index?type=all',
                icon: '/images/my_icon2.jpg'
            },
            opinionsAndSuggestions: {
                name: '意见建议',
                url: 'pages/user/proposal/index',
                icon: '/images/my_icon3.jpg'
            },
            aboutUs: {
                name: '关于我们',
                url: 'pages/user/aboutUs/index',
                icon: '/images/my_icon4.jpg'
            },
            contactUs: {
                name: '联系我们',
                url: '',
                icon: '/images/my_icon5.png'
            }
        },
        setInfo: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let _this = this;
        _this.setData({
            isLogin: App.checkIsLogin()
        });
        // 获取当前用户信息
        _this.getUserDetail();
        _this.getSetInfo()
    },
    getSetInfo() {
        let _this = this
        App._post_form('user.index/get_set_info', {}, function (result) {
            _this.setData({
                setInfo: result.data.setinfo
            })

        });
    },
    /**
     * 获取当前用户信息
     */
    getUserDetail() {
        let _this = this;
        App._get('user.index/detail', {}, function (result) {
            _this.setData(result.data);
        });
    },

    /**
     * 订单导航跳转
     */
    onTargetOrder(e) {
        let _this = this;
        if (!_this.onCheckLogin()) {
            return false;
        }
        // 记录formid
        App.saveFormId(e.detail.formId);
        let urls = {
            all: '/pages/order/index?type=all',
            payment: '/pages/order/index?type=payment',
            received: '/pages/order/index?type=received',
            refund: '/pages/order/refund/index',
        };
        // 转跳指定的页面
        wx.navigateTo({
            url: urls[e.currentTarget.dataset.type]
        })
    },

    /**
     * 菜单列表导航跳转
     */
    onTargetMenus(e) {
        // console.log(e, 'eee');
        let _this = this;
        if (!_this.onCheckLogin()) {
            return false;
        }
        // 记录formId
        App.saveFormId(e.detail.formId);
        if (e.target.dataset.url != '') {
            wx.navigateTo({
                url: '/' + e.target.dataset.url
            })
        } else {
            let phone = _this.data.setInfo.contact_us // 仅为示例，并非真实的电话号码
            wx.makePhoneCall({
                phoneNumber: phone
            })

        }

    },

    /**
     * 跳转我的钱包页面
     */
    onTargetWallet(e) {
        let _this = this;
        if (!_this.onCheckLogin()) {
            return false;
        }
        // 记录formId
        App.saveFormId(e.detail.formId);
        wx.navigateTo({
            url: './wallet/index'
        })
    },

    /**
     * 跳转积分明细页
     */
    onTargetPoints(e) {
        let _this = this;
        if (!_this.onCheckLogin()) {
            return false;
        }
        // 记录formId
        App.saveFormId(e.detail.formId);
        wx.navigateTo({
            url: '../points/log/index'
        });
    },

    /**
     * 跳转到登录页
     */
    onLogin() {
        wx.navigateTo({
            url: '../login/login',
        });
    },

    /**
     * 验证是否已登录
     */
    onCheckLogin() {
        let _this = this;
        if (!_this.data.isLogin) {
            App.showError('很抱歉，您还没有登录');
            return false;
        }
        return true;
    },
    // 去编辑资料页
    goEditData() {
        wx.navigateTo({
            url: '/pages/editdata/editdata',
        })
    },

})